<template>
  <el-container>
    <el-main class="bodys">
      <div class="coordinates-panel" :style="coordinatesFlag?'width: 360px':'width: 680px'">
        <el-row>
          <el-col :span="24">
            <el-col v-if="!coordinatesFlag" :span="12" class="coordinates-form1">
              <el-form label-width="50px" class="coordinates-body coordinates-total" label-position="left">
                <el-form-item class="item-label">
                  <div class="coordinates-div">
                    <div class="coordinates-title" style="margin-left: -200px">项目基点：</div>
                  </div>
                </el-form-item>
                <el-form-item class="item-label">
                  <div class="coordinates-div">
                    X：<el-input-number v-model="coordinatesPosition.positionX" v-viewinput="true" :controls="false" :min="-9999999999" :max="9999999999" :maxlength="15" class="coordinates-input" :disabled="true" />
                    <small class="coordinates-small">（mm）</small>
                  </div>
                </el-form-item>
                <el-form-item class="item-label">
                  <div class="coordinates-div">
                    Y：<el-input-number v-model="coordinatesPosition.positionY" v-viewinput="true" :controls="false" :min="-9999999999" :max="9999999999" :maxlength="15" class="coordinates-input" :disabled="true" />
                    <small class="coordinates-small">（mm）</small>
                  </div>
                </el-form-item>
                <el-form-item class="item-label">
                  <div class="coordinates-div">
                    Z：<el-input-number v-model="coordinatesPosition.positionZ" v-viewinput="true" :controls="false" :min="-9999999999" :max="9999999999" :maxlength="15" class="coordinates-input" :disabled="true" />
                    <small class="coordinates-small">（mm）</small>
                  </div>
                </el-form-item>
                <el-form-item class="item-label">
                  <div class="coordinates-div">
                    ∠：<el-input-number v-model="coordinatesPosition.rotationAngle" v-viewinput="true" :controls="false" :min="-180" :max="180" :maxlength="9" class="coordinates-input" :disabled="true" />
                    <small class="coordinates-small">（&nbsp;&nbsp;°&nbsp;&nbsp;）</small>
                  </div>
                </el-form-item>
              </el-form>
            </el-col>
            <el-col :span="coordinatesFlag?24:12" class="coordinates-form2">
              <div v-if="!coordinatesFlag" class="coordinates-title"> 子项坐标（相对值）： </div>
              <el-form ref="ruleForm" label-width="50px" class="coordinates-body" label-position="left" :model="subcoordinates" :rules="rules2">
                <el-form-item prop="positionX" class="item-label">
                  <div class="coordinates-div">
                    X：<el-input-number v-model="subcoordinates.positionX" :controls="false" :min="-9999999999" :max="9999999999" :maxlength="15" class="coordinates-input" />
                    <small class="coordinates-small">（mm）</small>
                  </div>
                </el-form-item>
                <el-form-item class="item-label" prop="positionY">
                  <div class="coordinates-div">
                    Y：<el-input-number v-model="subcoordinates.positionY" :controls="false" :min="-9999999999" :max="9999999999" :maxlength="15" class="coordinates-input" />
                    <small class="coordinates-small">（mm）</small>
                  </div>
                </el-form-item>
                <el-form-item class="item-label" prop="positionZ">
                  <div class="coordinates-div">
                    Z：<el-input-number v-model="subcoordinates.positionZ" :controls="false" :min="-9999999999" :max="9999999999" :maxlength="15" class="coordinates-input" />
                    <small class="coordinates-small">（mm）</small>
                  </div>
                </el-form-item>
                <el-form-item class="item-label" prop="rotationAngle">
                  <div class="coordinates-div">
                    ∠：<el-input-number v-model="subcoordinates.rotationAngle" class="coordinates-input" :min="-180" :max="180" :maxlength="9" :controls="false" @input.native="changeInput($event)" />
                    <small class="coordinates-small">（&nbsp;&nbsp;°&nbsp;&nbsp;）</small>
                  </div>
                </el-form-item>
              </el-form>
              <div v-if="coordinatesFlag" class="coordinates-notes">{{ notePosition }}</div>
              <div v-else class="coordinates-notes">{{ noteCoordinates }}</div>
              <div class="coordinates-container">
                <i class="el-icon-warning" style="color: #F56C6C" /><strong style="color: #F56C6C;font-size: 12px;">重要提示</strong>
                <p class="coordinates-description">{{ coordinatesFlag?msgCoordinates:msgPosition }}</p>
              </div>
            </el-col>
          </el-col>
        </el-row>
      </div>
      <div class="content-footer" style="justify-content:right">
        <el-button @click="clearValidate">取消</el-button>
        <el-button type="primary" size="mini" :loading="isLoading" @click="setPositon">确认</el-button>
      </div>
    </el-main>
  </el-container>
</template>

<script>

import { addCoordinates } from '@/api/project/projectSub'
import { setLoading } from '@/utils'

export default {
  name: 'GeneralCoordinates',
  props: {
    childParams: {
      type: Object
    },
    coordinatesPosition: {
      type: Object
    },
    subPositon: {
      type: Object
    },
    subNo: {
      type: String
    }
  },
  data() {
    this.setLoading = setLoading.bind(this)
    // 最多输入1位小数
    const validateDecimal = (rule, value, callback) => {
      if (!/^-?\d+(\.\d{1})?$/.test(value)) {
        callback(new Error('最多输入1位小数'))
      } else {
        callback()
      }
    }
    // 最多输入6位小数
    const validateRotation = (rule, value, callback) => {
      if (!/^-?\d+(\.\d{1,6})?$/.test(value)) {
        callback(new Error('最多输入6位小数'))
      } else {
        callback()
      }
    }
    return {
      rules2: {
        positionX: [
          { required: true, message: 'X值为数字且不能为空', type: 'number', trigger: 'blur' },
          { minlength: 1, maxlength: 11, message: '长度在1到11个数字', type: 'number', trigger: 'blur' },
          { validator: validateDecimal, trigger: 'blur' }
        ],
        positionY: [
          { required: true, message: 'Y值为数字且不能为空', type: 'number', trigger: 'blur' },
          { minlength: 1, maxlength: 11, message: '长度在1到11个数字', type: 'number', trigger: 'blur' },
          { validator: validateDecimal, trigger: 'blur' }
        ],
        positionZ: [
          { required: true, message: 'Z值为数字且不能为空', type: 'number', trigger: 'blur' },
          { minlength: 1, maxlength: 11, message: '长度在1到11个数字', type: 'number', trigger: 'blur' },
          { validator: validateDecimal, trigger: 'blur' }
        ],
        rotationAngle: [
          { required: true, message: '∠值为数字且不能为空', trigger: 'blur' },
          { minlength: 1, maxlength: 9, message: '长度在1到9个数字', type: 'number', trigger: 'blur' },
          { validator: validateRotation, trigger: 'blur' }
        ]
      },
      isLoading: false, // 按钮加载状态
      subcoordinates: {},
      coordinatesFlag: false, // 判断是否为总图页面
      subId: null,
      msgCoordinates: ` 项目基点即为本项目的统一参照点，目的为实现在hub中整合各子项模型。
注：由于项目基点、子项相对坐标值变动，历史批注将错位或失效！`, // 提示信息
      msgPosition: ` 注：由于项目基点、子项相对坐标值变动，历史批注将错位或失效！`,
      noteCoordinates: `请输入基于项目基点的相对值用于子项模型定位，包含X轴、Y轴、Z轴、以及角度。`, // 提示信息
      notePosition: `"∠"为基于正北方向的偏移角度值。` // 提示信息
    }
  },
  watch: {
    childParams: {
      handler: function(val) {
        this.subId = val.subId
        if (val.subNo === '9999') {
          this.coordinatesFlag = true
        } else {
          this.coordinatesFlag = false
        }
        this.$nextTick(() => {
          this.$refs.ruleForm.clearValidate()
        })
      },
      immediate: true,
      deep: true
    },
    coordinatesPosition: {
      handler: function(val) {
        if (this.subNo === '9999') {
          this.subcoordinates = this.coordinatesPosition
        } else {
          this.subcoordinates = this.subPositon
        }
      },
      immediate: true,
      deep: true
    },
    subPositon: {
      handler: function(val) {
        if (this.subNo !== '9999') {
          this.subcoordinates = this.subPositon
        } else {
          this.subcoordinates = this.coordinatesPosition
        }
      },
      immediate: true,
      deep: true
    }
  },
  methods: {
    /* 限制位数和小数点*/
    changeInput(e) {
      if (e.target.value.indexOf('.') >= 0) {
        e.target.value = e.target.value.substring(0, e.target.value.indexOf('.') + 11)
      }
    },
    /* 关闭及清除校检*/
    clearValidate() {
      this.$refs.ruleForm.clearValidate()
      this.$emit('update:coordinatesShow', false)
    },
    setPositon() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.setLoading(true)
          const params = {
            positionX: this.subcoordinates.positionX,
            positionY: this.subcoordinates.positionY,
            positionZ: this.subcoordinates.positionZ,
            rotationAngle: this.subcoordinates.rotationAngle,
            subId: this.subId
          }
          addCoordinates(params).then(() => {
            this.setLoading(false)
            this.$emit('update:coordinatesShow', false)
            this.$emit('refreshSubInfo', this.coordinatesFlag)
          }).catch(() => {
            this.setLoading(false)
          })
        } else {
          this.setLoading(false)
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.bodys{
  padding: 0;
  .coordinates-panel{
    display: inline-block !important;
    .el-row{
      margin: 0;
      padding: 0;
      .el-col{
        padding: 0;
        margin: 0;
        .coordinates-body{
          padding: 0;
          font-size: 8px;
          .item-label{
            .coordinates-div{
              display: block;
              .coordinates-input{
                width: 200px;
                display: inline-block;
                border-radius: 5px;
                opacity: 1;
                background: var(--el-color-white);
                box-sizing: border-box;
                border: 1px  #000000;
              }
              .coordinates-small{
                font-weight: bold;
                display: inline-block;
              }
              .coordinates-title{
                font-weight: bold;
              }
            }
          }
        }
        .coordinates-form1{
          text-align: center;
          padding: 0;
          margin-left: -36px;
        }
        .coordinates-form2{
          .coordinates-container{
            margin: 0px 0px 0 50px;
            padding: 10px 0 0 0;
            font-size: 14px;
            white-space: pre-wrap;
            line-height: 20px;
            .coordinates-description{
              margin-top: 10px;
            }
          }
          .coordinates-notes{
            padding: 0;
            font-size: 12px;
            margin-left: 50px;
            line-height: 20px;
          }
          ::v-deep .el-form-item__error{
            margin-left: 75px;
          }
          .coordinates-title{
            margin-left: 45px;
            font-weight: bold;
            margin-bottom: 10px;
          }
        }
      }
    }
  }
  .content-footer {
    position: relative;
    height: 35px;
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    justify-content: center;
    align-items: center;
    align-content: center;
    overflow: hidden;
    text-align: center;
    box-sizing: border-box;
    border: none;
  }
}
</style>
<style lang="scss">
.abow_dialog[view-coordinates] {
  .el-dialog {
    width: max-content;
    .el-dialog__body{
      padding: 20px 20px 18px 10px !important;
    }
  }
}
</style>
